// GPIOA->MODER |= GPIO_MODER_MODE12_0;                     /* Set GPIO mode to output */
// GPIOA->MODER &= ~GPIO_MODER_MODE12_1;                     /* Set GPIO mode to output */
// GPIOA->OTYPER &= ~GPIO_OTYPER_OT12;                       /* Set GPIO type to push-pull */
// GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEED12;                /* Set GPIO speed to high */
// GPIOA->ODR &= ~GPIO_ODR_OD12;

// GPIOA->MODER |= GPIO_MODER_MODE0_0;                     /* Set GPIO mode to output */
// GPIOA->MODER &= ~GPIO_MODER_MODE0_1;                     /* Set GPIO mode to output */
// GPIOA->OTYPER &= ~GPIO_OTYPER_OT0;                       /* Set GPIO type to push-pull */
// GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEED0;                /* Set GPIO speed to high */
// GPIOA->ODR &= ~GPIO_ODR_OD0;

// GPIOA->MODER |= GPIO_MODER_MODE3_0;                     /* Set GPIO mode to output */
// GPIOA->MODER &= ~GPIO_MODER_MODE3_1;                     /* Set GPIO mode to output */
// GPIOA->OTYPER &= ~GPIO_OTYPER_OT3;                       /* Set GPIO type to push-pull */
// GPIOA->OSPEEDR |= GPIO_OSPEEDR_OSPEED3;                /* Set GPIO speed to high */
// GPIOA->ODR &= GPIO_ODR_OD3;
#include "led.h"
#include "main.h"

u8 LEDs_Size = 8;
LED LEDs[] = {LED0_PIN, LED1_PIN, LED2_PIN, LED3_PIN, LED4_PIN, LED5_PIN, LED6_PIN, LED7_PIN};
u8 LEDs_Green_Size = 7;
LED LEDs_Green[] = {LED1_PIN, LED2_PIN, LED3_PIN, LED4_PIN, LED5_PIN, LED6_PIN, LED7_PIN};

void LED_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    // 使能 GPIOA 时钟
    __HAL_RCC_GPIOA_CLK_ENABLE();

    GPIO_InitStruct.Pin = LED_MAIN_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_WritePin(GPIOA, LED_MAIN_PIN, GPIO_PIN_RESET);
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    // 初始化 LED 引脚
    GPIO_InitStruct.Pin = LED_MAIN_PIN | LED0_PIN | LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN;
    // 关闭所有 LED
    HAL_GPIO_WritePin(GPIOA, LED0_PIN | LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN | LED5_PIN | LED6_PIN | LED7_PIN, GPIO_PIN_SET);
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    GPIO_InitStruct.Pin = LED0_PIN | LED1_PIN | LED2_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

    int i = 0;
    for (i = 0; i < 3; i++) {
        LED_Off(LEDs[i]);
    }
}

void LED_Main_Close(void)
{
    HAL_GPIO_WritePin(GPIOA, LED_MAIN_PIN, GPIO_PIN_SET);
    LED_OffAll(LEDs_Green, LEDs_Green_Size);
}

void LED_On(LED led)
{
    HAL_GPIO_WritePin(LED_PORT, led, GPIO_PIN_RESET);
}
void LED_Off(LED led)
{
    HAL_GPIO_WritePin(LED_PORT, led, GPIO_PIN_SET);
}

void LED_Toggle(LED led)
{
    HAL_GPIO_TogglePin(LED_PORT, led);
}

void LED_OnAll(LED leds[], u8 size)
{
    for (u8 i = 0; i < size; i++) {
        LED_On(leds[i]);
    }
}

void LED_OffAll(LED leds[], u8 size)
{
    for (u8 i = 0; i < size; i++) {
        LED_Off(leds[i]);
    }
}

void LED_Odd_Even_Toggle(u8 cur)
{
    for (size_t i = 0; i < LEDs_Green_Size; i++) {
        if (cur == 0) {
            if (i % 2 == 0) {
                LED_On(LEDs_Green[i]);
            } else {
                LED_Off(LEDs_Green[i]);
            }
        } else {
            if (i % 2 == 0) {
                LED_Off(LEDs_Green[i]);
            } else {
                LED_On(LEDs_Green[i]);
            }
        }
        // if (i % 2 == 0)
        // {
        //     if (cur == 0)
        //     {
        //         LED_On(LEDs_Green[i]);
        //     }
        //     else
        //     {
        //         LED_Off(LEDs_Green[i]);
        //     }
        // }
        // else
        // {
        //     if (cur == 0)
        //     {
        //         LED_Off(LEDs_Green[i]);
        //     }
        //     else
        //     {
        //         LED_On(LEDs_Green[i]);
        //     }
        // }
        // HAL_Delay(1);
    }
}